package com.yunqiao.admin.modules.api.controller;

import cn.hutool.core.lang.Dict;
import com.yunqiao.admin.modules.sys.controller.AbstractController;
import com.yunqiao.common.utils.PageUtils;
import com.yunqiao.common.utils.R;
import com.yunqiao.service.api.UserService;
import com.yunqiao.service.api.entity.UserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 用户表
 *
 * @author Crocutax
 * @email wangxw725@163.com
 * @date 2019-03-02 15:03:56
 */
@RestController
@RequestMapping("api/user")
@Api
public class UserController extends AbstractController {

    @Autowired
    private UserService userService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("sysType", true);
        PageUtils page = userService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        UserEntity user = userService.details(id);
        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserEntity user){
        userService.save(user);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserEntity user){
        userService.updateById(user);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
        userService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 获取司机地理位置
     * @return
     */
    @GetMapping("/getPosition")
    public R getPosition() {
        List<Dict> list = userService.getPosition();
        return R.objectOk(list);
    }
    @PutMapping("agent/{id}")
    @ApiOperation("同意申请司机")
    public R agent(@PathVariable Long id) {
        this.userService.agent(id);
        return R.ok();
    }

    @PutMapping("refuse")
    @ApiModelProperty("拒绝成为司机")
    public R refuse(@RequestBody UserEntity userEntity) {
        this.userService.refuse(userEntity);
        return R.ok();
    }
}
